package com.aloda.ojquestion.controller;

import cn.hutool.core.bean.BeanUtil;
import com.aloda.ojcommon.anno.AuthCheck;
import com.aloda.ojcommon.constant.ErrorCode;
import com.aloda.ojcommon.domain.enums.UserRoleEnum;
import com.aloda.ojcommon.domain.result.PageResult;
import com.aloda.ojcommon.domain.result.Result;
import com.aloda.ojquestion.pojo.dto.QuestionSubmitDTO;
import com.aloda.ojquestion.pojo.dto.QuestionSubmitPageDTO;
import com.aloda.ojquestion.pojo.vo.QuestionSubmitDetailVO;
import com.aloda.ojquestion.pojo.vo.QuestionSubmitPageVO;
import com.aloda.ojquestion.pojo.vo.QuestionSubmitVO;
import com.aloda.ojquestion.service.QuestionSubmitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author aloda
 * @Date 2025/4/27 15:58
 */
@Slf4j
@RestController
@RequestMapping("/question/submit")
public class QuestionSubmitController {

    @Resource
    private QuestionSubmitService questionSubmitService;

    /**
     * 提交题目
     *
     * @return
     */
    @PostMapping
    public Result<QuestionSubmitVO> submitQuestion(@RequestBody QuestionSubmitDTO questionSubmitDTO) {
        if (BeanUtil.isEmpty(questionSubmitDTO)) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        questionSubmitService.checkQuestionSubmit(questionSubmitDTO);
        log.info("提交题目:{}", questionSubmitDTO);
        QuestionSubmitVO questionSubmitVO = questionSubmitService.submitQuestion(questionSubmitDTO);
        return Result.success(questionSubmitVO);
    }

    /**
     * 分页查询
     *
     * @param questionSubmitPageDTO
     * @return
     */
    @PostMapping("/page")
    public Result<PageResult<QuestionSubmitPageVO>> pageQuery(@RequestBody QuestionSubmitPageDTO questionSubmitPageDTO) {
        if (BeanUtil.isEmpty(questionSubmitPageDTO)) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        PageResult<QuestionSubmitPageVO> pageResult = questionSubmitService.pageQuery(questionSubmitPageDTO);
        return Result.success(pageResult);
    }

    /**
     * 通过id查询答题记录详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<QuestionSubmitDetailVO> getDetailById(@PathVariable Long id) {
        return Result.success(QuestionSubmitDetailVO.of(questionSubmitService.getById(id)));
    }

    /**
     * 批量删除答题记录
     * @param ids
     * @return
     */
    @AuthCheck(role = UserRoleEnum.ADMIN)
    @DeleteMapping
    public Result<Boolean> delete(@RequestBody List<Long> ids) {
        return Result.success( questionSubmitService.removeBatchByIds(ids));
    }
}
